home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / SentenceBreakData.java < prev    next >
Text File  |  1998-09-22  |  19KB  |  376 lines

  1. /*
  2.  * @(#)SentenceBreakData.java    1.9 98/03/05
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. /**
  34.  * The SentenceBreakData contains data used by SimpleTextBoundary
  35.  * to determine sentence breaks.
  36.  * @see #BreakIterator
  37.  */
  38. final class SentenceBreakData extends TextBoundaryData
  39. {
  40.     private static final byte other = 0;
  41.     // lower case letters, digits...
  42.     private static final byte space = 1;
  43.     // spaces...
  44.     private static final byte terminator = 2;
  45.     // period, questionmark...
  46.     private static final byte ambiguosTerm = 3;
  47.     // Ambiguos terminator
  48.     private static final byte openBracket = 4;
  49.     // open brackets
  50.     private static final byte closeBracket = 5;
  51.     // close brackets
  52.     private static final byte cjk = 6;
  53.     // Characters where the previous sentence does not have a space
  54.     // after a terminator. Common in Japanese, Chinese, and Korean
  55.     private static final byte paragraphBreak = 7;
  56.     // Paragraph break
  57.     private static final byte lowerCase = 8;
  58.     // Lower case
  59.     private static final byte upperCase = 9;
  60.     private static final byte number = 10;
  61.  
  62.     private static final byte quote = 11;
  63.  
  64.     private static final byte sent_cr = 12;
  65.     private static final byte nsm = 13;
  66.     private static final byte EOS = 14;
  67.  
  68.     // digit
  69.     private static final int COL_COUNT = 15;
  70.  
  71.     private static final byte SI = (byte)0x80;
  72.     private static final byte STOP = (byte) 0;
  73.     private static final byte SI_STOP = (byte)SI + STOP;
  74.  
  75.     private static final byte kSentenceForwardData[] =
  76.     {
  77.         // other       space          terminator     ambTerm
  78.         // open        close          CJK            PB
  79.         // lower       upper          digit          Quote
  80.         // cr          nsm            EOS
  81.  
  82.         // 0
  83.         STOP,          STOP,          STOP,          STOP,
  84.         STOP,          STOP,          STOP,          STOP,
  85.         STOP,          STOP,          STOP,          STOP,
  86.         STOP,          STOP,          STOP,
  87.  
  88.         // 1
  89.         (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+2),  (byte)(SI+5),
  90.         (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+4),
  91.         (byte)(SI+1),  (byte)(SI+8),  (byte)(SI+9),  (byte)(SI+1),
  92.         (byte)(SI+10), (byte)(SI+1),  SI_STOP,
  93.  
  94.         // 2
  95.         SI_STOP,       (byte)(SI+3),  (byte)(SI+2),  (byte)(SI+5),
  96.         (byte)(SI+1),  (byte)(SI+2),  SI_STOP,       (byte)(SI+4),
  97.         SI_STOP,       SI_STOP,       SI_STOP,       (byte)(SI+2),
  98.         (byte)(SI+10), (byte)(SI+2),  SI_STOP,
  99.  
  100.         // 3
  101.         SI_STOP,       (byte)(SI+3),  SI_STOP,       SI_STOP,
  102.         SI_STOP,       SI_STOP,       SI_STOP,       (byte)(SI+4),
  103.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  104.         (byte)(SI+10), (byte)(SI+3),  SI_STOP,
  105.  
  106.         // 4
  107.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  108.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  109.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  110.         SI_STOP,       SI_STOP,       SI_STOP,
  111.  
  112.         // 5
  113.         SI_STOP,       (byte)(SI+6),  (byte)(SI+2),  (byte)(SI+5),
  114.         (byte)(SI+1),  (byte)(SI+5),  SI_STOP,       (byte)(SI+4),
  115.         (byte)(SI+1),  SI_STOP,       SI_STOP,       (byte)(SI+5),
  116.         (byte)(SI+10), (byte)(SI+5),  SI_STOP,
  117.  
  118.         // 6
  119.         SI_STOP,       (byte)(SI+6),  SI_STOP,       SI_STOP,
  120.         (byte)(SI+7),  (byte)(SI+1),  SI_STOP,       (byte)(SI+4),
  121.         (byte)(SI+1),  SI_STOP,       (byte)(SI+1),  SI_STOP,
  122.         (byte)(SI+10), (byte)(SI+6),  SI_STOP,
  123.  
  124.         // 7
  125.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  126.         (byte)(7),     SI_STOP,       SI_STOP,       SI_STOP,
  127.         (byte)(SI+1),  STOP,          SI_STOP,       SI_STOP,
  128.         SI_STOP,       (byte)(SI+7),  SI_STOP,
  129.  
  130.         // 8
  131.         (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+2),  (byte)(SI+8),
  132.         (byte)(SI+1),  (byte)(SI+5),  SI_STOP,       (byte)(SI+4),
  133.         (byte)(SI+1),  (byte)(SI+8),  (byte)(SI+9),  (byte)(SI+5),
  134.         (byte)(SI+10), (byte)(SI+8),  SI_STOP,
  135.  
  136.         // 9
  137.         (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+2),  (byte)(SI+9),
  138.         (byte)(SI+1),  (byte)(SI+5),  SI_STOP,       (byte)(SI+4),
  139.         (byte)(SI+1),  (byte)(SI+1),  (byte)(SI+9),  (byte)(SI+5),
  140.         (byte)(SI+10), (byte)(SI+9),  SI_STOP,
  141.  
  142.         // 10
  143.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  144.         SI_STOP,       SI_STOP,       SI_STOP,       (byte)(SI+4),
  145.         SI_STOP,       SI_STOP,       SI_STOP,       SI_STOP,
  146.         SI_STOP,       SI_STOP,       SI_STOP
  147.     };
  148.  
  149.     private static final WordBreakTable kSentenceForward
  150.         = new WordBreakTable(COL_COUNT, kSentenceForwardData);
  151.  
  152.     private static final byte kSentenceBackwardData[] =
  153.     {
  154.         // other       space          terminator     ambTerm
  155.         // open        close          CJK            PB
  156.         // lower       upper          digit          quote
  157.         // cr          nsm            EOS
  158.  
  159.         // 0
  160.         STOP,          STOP,          STOP,          STOP,
  161.         STOP,          STOP,          STOP,          STOP,
  162.         STOP,          STOP,          STOP,          STOP,
  163.         STOP,          STOP,          STOP,
  164.  
  165.         // 1
  166.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+2),
  167.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+3),  STOP,
  168.         (byte)(SI+2),  (byte)(SI+3),  (byte)(SI+2),  (byte)(SI+2),
  169.         (byte)(SI+2),  (byte)(SI+1),  STOP,
  170.  
  171.         // 2
  172.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+2),
  173.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+3),  STOP,
  174.         (byte)(SI+2),  (byte)(SI+3),  (byte)(SI+2),  (byte)(SI+2),
  175.         STOP,          (byte)(SI+2),  STOP,
  176.  
  177.         // 3
  178.         (byte)(SI+2),  (byte)(SI+4),  (byte)(SI+2),  (byte)(SI+2),
  179.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+3),  STOP,
  180.         (byte)(SI+3),  (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+2),
  181.         STOP,          (byte)(SI+3),  STOP,
  182.  
  183.         // 4
  184.         (byte)(SI+2),  (byte)(SI+4),  SI_STOP,       SI_STOP,
  185.         (byte)(SI+2),  (byte)(SI+2),  (byte)(SI+3),  STOP,
  186.         (byte)(SI+2),  (byte)(SI+3),  (byte)(SI+2),  (byte)(SI+2),
  187.         STOP,          (byte)(SI+4),  STOP
  188.     };
  189.  
  190.     private static final WordBreakTable kSentenceBackward
  191.         = new WordBreakTable(COL_COUNT, kSentenceBackwardData);
  192.  
  193.     private static final int kRawMapping[] =
  194.     {
  195.         other,        // UNASSIGNED             = 0,
  196.         upperCase,    // UPPERCASE_LETTER       = 1,
  197.         lowerCase,    // LOWERCASE_LETTER       = 2,
  198.         other,        // TITLECASE_LETTER       = 3,
  199.         other,        // MODIFIER_LETTER        = 4,
  200.         other,        // OTHER_LETTER           = 5,
  201.         nsm,          // NON_SPACING_MARK       = 6,
  202.         nsm,          // ENCLOSING_MARK         = 7,
  203.         other,        // COMBINING_SPACING_MARK = 8,
  204.         number,       // DECIMAL_DIGIT_NUMBER   = 9,
  205.         number,       // LETTER_NUMBER          = 10,
  206.         number,       // OTHER_NUMBER           = 11,
  207.         space,        // SPACE_SEPARATOR        = 12,
  208.         space,        // LINE_SEPARATOR         = 13,
  209.         space,        // PARAGRAPH_SEPARATOR    = 14,            ???????
  210.         other,        // CONTROL                = 15,
  211.         other,        // PRIVATE_USE            = 16,
  212.         other,        // FORMAT                 = 17,
  213.         other,        // ????                   = 18,
  214.         other,        // SURROGATE              = 19,
  215.         other,        // DASH_PUNCTUATION       = 20,
  216.         openBracket,  // START_PUNCTUATION      = 21,
  217.         closeBracket, // END_PUNCTUATION        = 22,
  218.         other,        // CONNECTOR_PUNCTUATION  = 23,
  219.         other,        // OTHER_PUNCTUATION      = 24,
  220.         other,        // MATH_SYMBOL            = 25,
  221.         other,        // CURRENCY_SYMBOL        = 26,
  222.         other,        // MODIFIER_SYMBOL        = 27,
  223.         other,        // OTHER_SYMBOL           = 28;
  224.     };
  225.  
  226.     private static SpecialMapping kExceptionChar[] =
  227.     {
  228.         //note: the ranges in this table must be sorted in ascending order
  229.         //as required by the UnicodeClassMapping class.
  230.         new SpecialMapping(ASCII_HORIZONTAL_TABULATION, space),
  231.         new SpecialMapping(ASCII_LINEFEED, space),
  232.         new SpecialMapping(ASCII_FORM_FEED, terminator),
  233.         new SpecialMapping(ASCII_CARRIAGE_RETURN, space),
  234.  
  235.         new SpecialMapping(ASCII_EXCLAMATION_MARK, terminator),
  236.         new SpecialMapping(ASCII_QUOTATION_MARK, quote),
  237.  
  238.         new SpecialMapping(ASCII_APOSTROPHE, quote),
  239.  
  240.         new SpecialMapping(ASCII_FULL_STOP, ambiguosTerm),
  241.         new SpecialMapping(ASCII_QUESTION_MARK, terminator),
  242.         new SpecialMapping(ASCII_NONBREAKING_SPACE, other),
  243.         new SpecialMapping(PUNCTUATION_LINE_SEPARATOR, space),
  244.         new SpecialMapping(PUNCTUATION_PARAGRAPH_SEPARATOR, paragraphBreak),
  245.         new SpecialMapping(PUNCTUATION_IDEOGRAPHIC_FULL_STOP, terminator),
  246.         new SpecialMapping(HIRAGANA_LETTER_SMALL_A, HIRAGANA_LETTER_VU, cjk),
  247.         new SpecialMapping(COMBINING_KATAKANA_HIRAGANA_VOICED_SOUND_MARK,
  248.                            HIRAGANA_SEMIVOICED_SOUND_MARK, cjk),         // cjk
  249.         new SpecialMapping(KATAKANA_LETTER_SMALL_A, KATAKANA_LETTER_SMALL_KE,
  250.                            cjk),   // cjk
  251.         new SpecialMapping(UNICODE_LOW_BOUND_HAN, UNICODE_HIGH_BOUND_HAN, cjk),
  252.         new SpecialMapping(CJK_COMPATIBILITY_F900, CJK_COMPATIBILITY_FA2D,cjk),
  253.         new SpecialMapping(UNICODE_ZERO_WIDTH_NON_BREAKING_SPACE, other),
  254.         new SpecialMapping(END_OF_STRING, EOS)
  255.     };
  256.  
  257.     private static final boolean SentenceExceptionFlags[] = {
  258.         false,            // kNonCharacter         = 0,
  259.         false,            // kUppercaseLetter      = 1,
  260.         false,            // kLowercaseLetter      = 2,
  261.         false,            // kTitlecaseLetter      = 3,
  262.         false,            // kModifierLetter       = 4,
  263.         true,             // kOtherLetter          = 5,
  264.         true,             // kNonSpacingMark       = 6,
  265.         false,            // kEnclosingMark        = 7,
  266.         false,            // kCombiningSpacingMark = 8,
  267.         false,            // kDecimalNumber        = 9,
  268.         false,            // kLetterNumber         = 10,
  269.         false,            // kOtherNumber          = 11,
  270.         true,             // kSpaceSeparator       = 12,
  271.         true,             // kLineSeparator        = 13,
  272.         true,             // kParagraphSeparator   = 14,
  273.         true,             // kControlCharacter     = 15,
  274.         true,             // kFormatCharacter      = 16,
  275.         false,            // UNDEFINED             = 17,
  276.         false,            // kPrivateUseCharacter  = 18,
  277.         false,            // kSurrogate            = 19,
  278.         false,            // kDashPunctuation      = 20,
  279.         false,            // kOpenPunctuation      = 21,
  280.         false,            // kClosePunctuation     = 22,
  281.         false,            // kConnectorPunctuation = 23,
  282.         true,             // kOtherPunctuation     = 24,
  283.         false,            // kMathSymbol           = 25,
  284.         false,            // kCurrencySymbol       = 26,
  285.         false,            // kModifierSymbol       = 27,
  286.         false             // kOtherSymbol          = 28
  287.     };
  288.  
  289.     private static final int kSentenceAsciiValues[] = {
  290.         //  null    soh     stx     etx     eot     enq     ask     bell
  291.             other,  other,  other,  other,  other,  other,  other,  other,
  292.         //  bs      ht      lf     vt     ff          cr     so     si
  293.             other,  space,  space, other, terminator, space, other, other,
  294.         //  dle     dc1     dc2     dc3     dc4     nak     syn     etb
  295.             other,  other,  other,  other,  other,  other,  other,  other,
  296.         //  can     em      sub     esc     fs      gs      rs      us
  297.             other,  other,  other,  other,  other,  other,  other,  other,
  298.         //  sp      !           "      #      $      %      &      '
  299.             space,  terminator, quote, other, other, other, other, quote,
  300.         //  (            )             *      +      ,      -      .             /
  301.             openBracket, closeBracket, other, other, other, other, ambiguosTerm, other,
  302.         //  0       1       2       3       4       5       6       7
  303.             number, number, number, number, number, number, number, number,
  304.         //  8       9       :       ;       <       =       >       ?
  305.             number, number, other,  other,  other,  other,  other,  terminator,
  306.         //  @       A          B          C          D          E          F          G
  307.             other,  upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase,
  308.         //  H          I          J          K          L          M          N          O
  309.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase,
  310.         //  P          Q          R          S          T          U          V          W
  311.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase,
  312.         //  X          Y          Z          [            \      ]             ^      _
  313.             upperCase, upperCase, upperCase, openBracket, other, closeBracket, other, other,
  314.         //  `       a          b          c          d          e          f          g
  315.             other,  lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase,
  316.         //  h          i          j          k          l          m          n          o
  317.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase,
  318.         //  p          q          r          s          t          u          v          w
  319.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase,
  320.         //  x          y          z          {            |      }             ~      del
  321.             lowerCase, lowerCase, lowerCase, openBracket, other, closeBracket, other, other,
  322.         //  ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl
  323.             other,  other,  other,  other,  other,  other,  other,  other,
  324.         //  ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl
  325.             other,  other,  other,  other,  other,  other,  other,  other,
  326.         //  ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl
  327.             other,  other,  other,  other,  other,  other,  other,  other,
  328.         //  ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl    ctrl
  329.             other,  other,  other,  other,  other,  other,  other,  other,
  330.         //  nbsp    í       ó       ú       ñ       Ñ       ª
  331.             other,  other,  other,  other,  other,  other,  other,  other,
  332.         //  ¿       ⌐       ¬          ½            ¼      ¡      «      »
  333.             other,  other,  lowerCase, openBracket, other, other, other, other,
  334.         //  ░       ▒       ▓       │       ┤       ╡          ╢      ╖
  335.             other,  other,  number, number, other,  lowerCase, other, other,
  336.         //  ╕       ╣          ║      ╗             ╝       ╜       ╛       ┐
  337.             other,  lowerCase, other, closeBracket, number, number, number, other,
  338.         //  └          ┴          ┬          ├          ─          ┼          ╞          ╟
  339.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase,
  340.         //  ╚          ╔          ╩          ╦          ╠          ═          ╬          ╧
  341.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase,
  342.         //  ╨          ╤          ╥          ╙          ╘          ╒          ╓          ╫
  343.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, other,
  344.         //  ╪          ┘          ┌          █          ▄          ▌          ▐          ▀
  345.             upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, upperCase, lowerCase,
  346.         //  α          ß          Γ          π          Σ          σ          µ          τ
  347.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase,
  348.         //  Φ          Θ          Ω          δ          ∞          φ          ε          ∩
  349.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase,
  350.         //  ≡          ±          ≥          ≤          ⌠          ⌡          ÷          ≈
  351.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, other,
  352.         //  °          ∙          ·          √          ⁿ          ²          ■           
  353.             lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase, lowerCase
  354.     };
  355.  
  356.     private static final UnicodeClassMapping kSentenceMap
  357.         = new UnicodeClassMapping(kRawMapping, kExceptionChar, SentenceExceptionFlags,
  358.         kSentenceAsciiValues);
  359.  
  360.     public WordBreakTable forward()
  361.     {
  362.         return kSentenceForward;
  363.     }
  364.  
  365.     public WordBreakTable backward()
  366.     {
  367.         return kSentenceBackward;
  368.     }
  369.  
  370.     public UnicodeClassMapping map()
  371.     {
  372.         return kSentenceMap;
  373.     }
  374. }
  375.  
  376.